﻿using System;
using System.Web.Security;
using Hadlow.Jobvertizer.Models;

namespace Hadlow.Jobvertizer.Controllers
{
    public class LoginController : BaseController
    {
        public void Index(string returnUrl)
        {
            PropertyBag["ReturnUrl"] = returnUrl;    
        }

        public void Login(string username, string password, bool rememberme, string returnUrl)
        {
            if (System.Web.Security.Membership.ValidateUser(username, password))
            {
                CancelView();

                FormsAuthentication.RedirectFromLoginPage(username, rememberme, Context.ApplicationPath);

                return;
            }

            // If we got here then something is wrong with the supplied username/password
            Flash["error"] = "Invalid user name or password. Try again.";
            RedirectToAction("Index", "ReturnUrl=" + returnUrl);
        }

        public void Logoff(string returnUrl)
        {
            FormsAuthentication.SignOut();
            System.Threading.Thread.CurrentPrincipal = Context.CurrentUser = User.Guest;

            // redirecing to returnUrl causes lots of problems, return to home for now...
            Redirect("home", "index");
        }
    }
}
